﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Table.DataServices;

namespace AzureSQLBackup
{
    class LogDataContext : TableServiceContext
    {
        public const string ErrorLogTable = "ErrorLogs";
        public const string EventLogTable = "EventLogs";

     

        public LogDataContext(CloudTableClient client)
            : base(client)
        {
            var table = client.GetTableReference(ErrorLogTable);
            table.CreateIfNotExists();
           
            table = client.GetTableReference(EventLogTable);
            table.CreateIfNotExists();
        }

        public IQueryable<ErrorLog> ErrorLogs
        {
            get
            {
                return this.CreateQuery<ErrorLog>(ErrorLogTable);
            }
        }

        public IQueryable<EventLog> EventLogs
        {
            get
            {
                return this.CreateQuery<EventLog>(EventLogTable);
            }
        }

        public void SaveError(string description)
        {
            this.SaveError(description, string.Empty, System.DateTime.Now);
        }

        public void SaveError(string description, string remarks)
        {
            this.SaveError(description, remarks, System.DateTime.Now);
        }

        public void SaveError(string description, string remarks, DateTime errorTime)
        {
            this.AddObject(ErrorLogTable, new ErrorLog { Description = description, ErrorTime = errorTime, Remarks = remarks });
            this.SaveChanges();
        }

        public void SaveEvent(string description)
        {
            this.SaveEvent(description, string.Empty, System.DateTime.Now);
        }

        public void SaveEvent(string description, string remarks)
        {
            this.SaveEvent(description, remarks, System.DateTime.Now);
        }

        public void SaveEvent(string description, string remarks, DateTime errorTime)
        {
            this.AddObject(EventLogTable, new EventLog { Description = description, EventTime = errorTime, Remarks = remarks });
            this.SaveChanges();
        }
    }
}
